package com.newcapec.common.utils;

import com.alibaba.fastjson.JSONObject;

/**
 * 人脸平台响应数据
 *
 * @author steven
 */
public class AifaceResponse {

	/**
	 * 返回code
	 * 1成功，其它失败
	 */
	private Integer code;
	/**
	 * 失败消息
	 */
	private String msg;
	/**
	 * 成功时返回的json数据
	 */
	private JSONObject responseData;
	public AifaceResponse(String secretKey, String responseText){
		//请求返回消息体处理
		if(responseText == null){
			this.code = -1;
			this.msg = "请求失败,结果未知!";
		}else{
			JSONObject responseJson = JSONObject.parseObject(responseText);
			//请求失败
			if (responseJson.containsKey("errcode")) {
				//-1001	INTERFACE_NOTSUPPORT	接口内容暂不支持；主要是在某些部署模式下，部分接口内容不支持
				//-2001	INVALID_TOKEN	无效的token或者token已过期
				//-2002	INVALID_APPKEY_SECRET	无效的app_key或者app_secret
				//-3001	NOT_GRANTED	未授权的操作
//				if ("-2001".equals(retdatajson.getString("errcode"))) {
//					//token无效，清空token，下次在请求时，继续申请token
//					Constant.AIFACE_TOKEN = null;
//					this.code =-2001;
//				}else{
//					this.code = -1;
//					this.msg = retdatajson.getString("errmsg");
//				}
			}else{
				//请求成功
				//解密返回的data密文为明文
				String dataText = ThreeDESUtils.decryptMode(secretKey, responseJson.getString("data"));
				JSONObject responseData = JSONObject.parseObject(dataText);
				if(responseData.getIntValue("code") == 1){
					this.code = 1;
					this.responseData = responseData;
				}else{
					this.code = responseData.getIntValue("code");
					this.msg = responseData.getString("msg");
					this.responseData = responseData;
				}
			}
		}
	}
	public Integer getCode() {
		return code;
	}
	public void setCode(Integer code) {
		this.code = code;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public JSONObject getResponseData() {
		return responseData;
	}
	public void setResponseData(JSONObject responseData) {
		this.responseData = responseData;
	}
}
